package Demo09.Lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class RunnableImpl implements Runnable{
    private int ticket = 100;
    Lock l = new ReentrantLock();
    @Override
    public void run() {
        while(ticket >= 0){
            l.lock();
            if(ticket > 0){
                //提高安全问题出现的概率，让程序睡眠
                try {
                    Thread.sleep(10);
                    System.out.println(Thread.currentThread().getName() + "正在卖第" + ticket + "张票");
                    ticket--;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    l.unlock();
                }
            }
        }

    }
}
